/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bot_jae;

/**
 *
 * @author Eric Momsen
 */

import ai_util.MoveInfo;
import java.util.HashMap;
import java.util.Hashtable;
import jpl.Query;
import jpl.*;

public class Move {

    void abort_search() {
        //TODO what needs to be killed for a new game, any processes?
        
        //throw new UnsupportedOperationException("Not yet implemented");
    }

    //keep track of time, query prolog, keep track of iterative deepening.
    String genMove(ServerInfo info) {
        
        //test_0();
        
        String pl_board;  //board description to send to prolog
        String pl_move;  //result from Prolog search engine
        String move_aei;  //final result
        
        int max_search_depth = 20;
        
        
        
        
        
        
        
        //System.out.println("Working Directory = " System.getProperty("user.dir"));
            
            //get board string to pass to prolog
            pl_board = info.board.plString();
            
            
            
            //should this be done somewhere else, are we reloading knowledgebase every time?
            String t1 = "consult('simpleAgent.pl')";  
            Query q1 = new Query(t1);
            
            //even though we keep adding new variables, all jpl examples seem to start with this
            //so something in the Query object must be remembering the last file consulted?!
            
            Variable X = new Variable();  //jpl object
            Variable Y = new Variable();  // pl currently has a third parameter, not used
            
            
            
            //debuging:
            if(false){
                if ( !q1.hasSolution() ){
			System.out.println( "consult('simpleAgent.pl') failed" );
			System.exit( 1 );
		}
		System.out.println( "passed." );
            
                System.out.print( "test 1..a." );
                Query q2 = new Query("isStrongerOfTwo(cat,rabbit)");

		if ( !q2.hasSolution() ){
			System.out.println( "isStrongerOfTwo(cat,rabbit failed" );
			//System.exit( 1 );
		}
		System.out.println( "passed." );
                
                System.out.println(q2.hasSolution());
                
                System.out.print( "test 1..b." );
                q2 = new Query("isStrongerOfTwo(rabbit,cat)");

		if ( !q2.hasSolution() ){
			System.out.println( "isStrongerOfTwo(rabbit,cat) failed" );
			//System.exit( 1 );
		}
		System.out.println( "passed." );
                
                System.out.println(q2.hasSolution());
            
                //try now with a variable
                
                Variable Z = new Variable();
                System.out.println("test 3... query with variable");
                Query q3 = new Query(
                        "isStrongerOfTwo",
                        new Term[] {Z,new Atom("cat")}
                        );
                
                Hashtable sol3 = new Hashtable();
//                java.util.Hashtable sol3;
//                        Hashtable sol3;
                //HashMap sol3 = new HashMap();
                
                sol3 = q3.oneSolution();
                System.out.println( "Z = " + sol3.get(Z));
                
                //end test 3
                
                System.out.println("test 4....");
                
                String answer = "initialized";
                Query q4 = new Query("isStronger(horse,X)");
                
                System.out.println("has solution? " + q4.hasSolution());
                System.out.println("first solution? " + q4.oneSolution());
                
                Hashtable solution;
                while (q4.hasMoreSolutions()) {
                    solution = q4.nextSolution();
                    System.out.println("X = " + solution.get(X));
                }
                
                //answer = jpl.Query.oneSolution("isStrongerOfTwo(rabbit,X)").get("X").toString();
                //System.out.println("combined method: " + answer);
                
                //end test 4
                System.out.println("board state: " + pl_board);
                //test 5
                System.out.println("test 5....");
                
                Query q5 = new Query("move("+pl_board+",X,_)");
                System.out.println("move is: " + q5.oneSolution());
                
                System.out.println(pl_board);
                
                
                // prolog answer: {X=action(rabbit(0, 0), east)}
                //                012345678901234567
                String pa = "{X=action(rabbit(0, 0), east)}";
                String test5 = "";
                test5 += pa.charAt(pa.indexOf("(")+1);
                String dir = "";
                char testingindex = (pa.charAt(pa.indexOf("(",pa.indexOf("(")+1)+1));
                switch (pa.charAt(pa.indexOf("(",pa.indexOf("(")+1)+1)){
                    case '0':
                        dir = "a";
                        break;
                    case '1':
                        dir = "b";
                        break;
                    case '2':
                        dir = "c";
                        break;
                    case '3':
                        dir = "d";
                        break;
                    case '4':
                        dir = "e";
                        break;
                    case '5':
                        dir = "f";
                        break;
                    case '6':
                        dir = "g";
                        break;
                    case '7':
                        dir = "h";
                        break;
                }
                test5 += dir;
                char charDirIndex = pa.charAt(pa.indexOf(",")+2);
                int intDirIndex = Character.getNumericValue(charDirIndex)+1;
                //char actIndex = (char) intDirIndex;
                
                test5 += intDirIndex;
                
                test5 += pa.charAt(pa.indexOf(",",pa.indexOf(",")+2));
            }    
            //end dubugging
                
                
        
        
        
        
        if(info.board.ply_num<2){ //first move
            //
            // don't know side_to_move yet...  if(info.side_to_move == 0){
            if(info.board.ply_num == 0){
                move_aei = "Rh1 Rg1 Rf1 Rc1 Rb1 Ra1 Rh2 Ra2 Ce1 Cf2 Dd1 Dc2 Hg2 Hb2 Md2 Ee2";
            }
            else {
                move_aei = "ed7 hg7 hb7 me7 de8 dd8 cf7 cc7 ra7 rh7 ra8 rb8 rc8 rf8 rg8 rh8";
            }
        }
        else { //later moves
            //time management

            //depth

            //iterate over depth

            //OK, out of time, use last answer
            //How get java to keep track of time while waiting for answer, to go ahead with last one?

            
            /*JPL docs: Using the jpl.Variable class, we can construct a non ground query;
             * and using other methods of Query we can obtain a solution
             * in the form of a java.util.Hashtable.  If the Query has one or more solutions, 
             * then its Query.oneSolution() method returns a Hashtable representing 
             * the first solution, otherwise it returns null:
             */
           
            if(false){ //while testing interface, just the move
            Query q4 = new Query( 
                    "move", 
                    new Term[] {new Atom(pl_board), X, Y} 
                    );

            java.util.Hashtable solution;
            solution = q4.oneSolution();
            
            pl_move = solution.get(X).toString();
            
            System.out.println(pl_move);
            }
            /* JPL docs:
             * The HashTable contains bindings in the form of Terms, 
             * each of which is indexed by its corresponding Variable in the Query.
            */
            
            Query q5 = new Query("move("+pl_board+",X,_)");
                System.out.println("move is: " + q5.oneSolution());
            
            // sample from Prolog % RESULT: X = action(rabbit(4, 2), west).
            //convert prolog answer to correct move format
            
                String pa = q5.oneSolution().toString();
                String test5 = "";
                test5 += pa.charAt(pa.indexOf("(")+1);
                String dir = "";
                char testingindex = (pa.charAt(pa.indexOf("(",pa.indexOf("(")+1)+1));
                switch (pa.charAt(pa.indexOf("(",pa.indexOf("(")+1)+1)){
                    case '0':
                        dir = "a";
                        break;
                    case '1':
                        dir = "b";
                        break;
                    case '2':
                        dir = "c";
                        break;
                    case '3':
                        dir = "d";
                        break;
                    case '4':
                        dir = "e";
                        break;
                    case '5':
                        dir = "f";
                        break;
                    case '6':
                        dir = "g";
                        break;
                    case '7':
                        dir = "h";
                        break;
                }
                test5 += dir;
                char charDirIndex = pa.charAt(pa.indexOf(",")+2);
                int intDirIndex = Character.getNumericValue(charDirIndex)+1;
                //char actIndex = (char) intDirIndex;
                
                test5 += intDirIndex;
                
                test5 += pa.charAt(pa.indexOf(",",pa.indexOf(",")+2));

            
            
            //move_aei = "Ed2n Ed3n Ed4n Hb2n";
            move_aei = test5;
        }
        return move_aei;
        
    }
    private void test_0()
	{
		System.out.print( "test 0..." );

		Query query = 
			new Query("consult('test.pl')");

		if ( !query.hasSolution() ){
			System.out.println( "consult('test.pl') failed" );
			System.exit( 1 );
		}
		System.out.println( "passed." );
	}

}
